if (window.plus) {
	ready();
} else {
	document.addEventListener('plusready', ready, false);
}

$(function() {
	if (AGENT_CLIENT.WEB) {
		ready();
	}
})

function ready() {
	var user = null,
		thisPage = null,
		thisInit = new confirmorderInit(),
		thisEvent = new confirmorderEvent(),
		html = [];

	var confirmOrderPage = function() {
		this.init();
	};
	confirmOrderPage.prototype = {
		init: function() {
			this.initData();
			this.bindEvent();
		},
		initData: function() {
			user = getLocalStorage(STORAGE_MANAGER.user);
			if (!user || !user.id) {
				redirect("../account/login.html");
				return false;
			}
			var requestUrl = CONFIG_MANAGER.RequstUrl;			
			var requestData = getLocalStorage(STORAGE_MANAGER.confirmOrderData);
			if (1 == requestData.instant_buy) {
				requestUrl += "v1/order/buy";
			}else{
				requestUrl += "v1/order/confirm";
			}
			$.ajax({
				type: "POST",
				url: requestUrl,
				data: JSON.stringify( requestData ),
				contentType: "application/json",
				async: true,
				beforeSend:function(){ $.showWaiting(); }
			}).done(function(data) {
				//data.address = [];
				thisInit.paintAddress(data);
				thisInit.paingDelivery(data);
				thisInit.loadContent(data);
				thisInit.loadTotal(data);
				//收货地址
				var addresses = data.address;
				thisEvent.changeAddress(addresses);
				//选择其他配送方式
				thisEvent.chooseDeliver(user, data);
				//选择优惠券
				thisEvent.useCoupon(user, data);
				//提交订单
				thisEvent.commitOrder(user, data);
			}).fail(function(data) {
				var infor = JSON.parse(data);
				$.toast(infor.Message);
			}).always(function(){
				$.closeWaiting();
			});
		},
		bindEvent: function() {
			//点击后退
			$("#back").on("click", function() {
				back();
			});
		},
	};

	thisPage = new confirmOrderPage();

}